/*
    Matali Physics Demo
    Copyright (c) 2011 KOMIRES Sp. z o. o.
 */
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Komires.MataliPhysics;

namespace MataliPhysicsDemo
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Water
    {
        Demo demo;
        PhysicsScene scene;
        string instanceIndexName;

        public Water(Demo demo, int instanceIndex)
        {
            this.demo = demo;
            instanceIndexName = " " + instanceIndex.ToString();
        }

        public void Initialize(PhysicsScene scene)
        {
            this.scene = scene;
        }

        public static void CreateShapes(Demo demo, PhysicsScene scene, int cellCountX, int cellCountZ, float depth, float density, float linearDamping, float angularDamping, float surfaceDamping, float surfaceVelocityFactor, float surfaceAmplitudeFactor, float minSurfaceAmplitude, float maxSurfaceAmplitude, float margin, bool dynamic)
        {
            ShapePrimitive shapePrimitive = null;
            Shape shape = null;

            shapePrimitive = scene.Factory.ShapePrimitiveManager.Create("Water");
            shapePrimitive.CreateFluid(cellCountX, cellCountZ, depth, density, linearDamping, angularDamping, surfaceDamping, surfaceVelocityFactor, surfaceAmplitudeFactor, minSurfaceAmplitude, maxSurfaceAmplitude, dynamic);

            shape = scene.Factory.ShapeManager.Create("Water");
            shape.Set(shapePrimitive, Matrix.Identity, margin);
            shape.CreateMesh();

            if (!demo.Meshes.ContainsKey("Water"))
                demo.Meshes.Add("Water", new DemoMesh(demo, shape, null, Vector2.One, true, true, false, false, true, RasterizerState.CullNone, dynamic));
        }

        public void Create(Vector3 objectPosition, Vector3 objectScale, Quaternion objectOrientation)
        {
            Shape waterShape = scene.Factory.ShapeManager.Find("Water");

            PhysicsObject objectRoot = scene.Factory.PhysicsObjectManager.Create("Water " + instanceIndexName);
            objectRoot.EnableCursorInteraction = false;
            objectRoot.DrawPriority = 4;
            objectRoot.Material.SetAmbient(0.55f, 0.55f, 0.55f);
            objectRoot.Material.SetDiffuse(0.0f, 0.0f, 0.75f);
            objectRoot.Shape = waterShape;
            objectRoot.UserTagStr = "Water";
            objectRoot.InitLocalTransform.SetPosition(ref objectPosition);
            objectRoot.InitLocalTransform.SetScale(ref objectScale);
            objectRoot.InitLocalTransform.SetOrientation(ref objectOrientation);

            objectRoot.InternalControllers.CreateFluidController(true);

            scene.UpdateFromInitLocalTransform(objectRoot);
        }
    }
}
